/***
http 配置
*/
import axios from 'axios'
import store from './store/store.js'
import * as types from './store/types.js'
import router from './router/index.js'

//axios 配置
axios.defaults.timout = 5000
// axios.defaults.baseURL = 'http://console.yuejiadian.com'

// http request 拦截器
axios.interceptors.request.use(
  // 请求发送之前拦截
  config => {
    if(store.state.x_user_token) {
      config.headers = {'X-USER-UID': store.state.x_user_uid, 'X-USER-TOKEN': store.state.x_user_token}
    }
    return config
  },
  err => {
    return Promise.reject(err)
  }
);

// http response 拦截器
axios.interceptors.response.use(
  response => {
    return response
  },
  error => {
    if(error.response) {
      switch(error.response.status){
        case 401:
          // 401 清除 token 信息并跳转到登录界面
          // store.commit(types.LOGOUT)
          // console.log(router.currentRoute);
          router.replace({
            path: '/login',
            query: {redirect: router.currentRoute.query.redirect}
          })
      }
    }
    return Promise.reject(error.response.data)
  }
)

export default axios
